<?php




/**
 * 用户列表
 *
 */
function li($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;

    $view_var   = array();
    $tbl_job    = $_TABLES['hr_job'];
    $conn   = & get_db_conn();

    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);
    if($key != '') $where  .= "AND (job_name LIKE '%$key%') ";



    $is_ad  = @$_GET['is_ad'];
    if($is_ad == 1) {
        // 发布时间
        $begin_date = @$_GET['begin_date'];
        $end_date   = @$_GET['end_date'];
        if($begin_date == '') {
            $begin_date   = 0;
        } else {
            $tmp    = explode('-', $begin_date);
            $begin_date = mktime(0 ,0, 0,$tmp[1] ,$tmp[2], $tmp[0]);
        }
        if($end_date == '') {
            $end_date   = $timestamp;
        } else {
            $tmp        = explode('-', $end_date);
            $end_date   = mktime(23 ,59, 59,$tmp[1] ,$tmp[2], $tmp[0]);
        }
        $where .= "AND (send_time >= '$begin_date' AND send_time <= '$end_date' ) ";

    }

    // 分页处理开始
    $csql   = "SELECT COUNT(*) AS rs_count FROM $tbl_job $where";
    $rs     = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕


    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT * FROM $tbl_job
            $where 
            ORDER BY  $str_order_by id DESC 
            ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    //echo $sql;
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束

    //echo $sql;
	//exit;

    return $view_var;
}


function read($id = 0) {
    global $_TABLES, $timestamp;

    $view_var   = array();
    $tbl_job    = $_TABLES['hr_job'];
    if($id > 0) {
        $conn   = & get_db_conn();
        $sql    = "SELECT * FROM $tbl_job WHERE id = $id";
        $view_var   = $conn->GetRow($sql);
    } else {
        $view_var   = array(
            'id'        => 0,
            'job_name'  => '',
            'treatment' => '面议',
            'num'       => 0,
            'anynum'    => 0,
            'begin_time'=> $timestamp,
            'end_time'  => 0,
            'tel'       => '',
            'link_man'  => '',
            'require'   => '',
            'notice'    => '',
            'display'  => 0,
            'city'      => '成都',
            'anytime'   => 0,
            'create_time'=> $timestamp,
            'requirement' => '',
            );
    }
	
	
    return $view_var;
}

function request() {
    global $data, $timestamp, $G_big_integer;
        // 专题ID
    $requirement= $_POST['requirement'];
    $tmp        = array();
    if(is_array($requirement) && sizeof($requirement) > 0) {
        foreach($requirement as $v) {
            $tmp[]  = trim($v);
        }
        $requirement = $tmp;
    } else {
        $requirement = array(trim(@$_POST['requirement']));
    }
    $requirement = serialize($requirement);
    $data    = $_POST['job'];

    $data['id']         = @(int)$data['id'];
    $data['num']        = @(int)$data['num'];
    $data['anynum']     = @(int)$data['anynum'];
    $data['display']    = @(int)$data['display'];
    $data['anytime']    = @(int)$data['anytime'];
    $data['requirement']= $requirement; // require 是mysql的关键词


    // 人数
    if($data['num'] <= 0) {
        $data['num']    = 0;
        $data['anynum'] = 1;
    }
    if($data['anynum'] == 1) $data['num']    = 0;


    // 如果开始时间没有设置默认为当前时间
    if($data['begin_date'] == '') $data['begin_time'] = $timestamp;
    else {
        $arr_time   = explode('-', $data['begin_date']); // 年-月-日
        if(sizeof($arr_time) == 3) {
            $month  = @(int)$arr_time[1];
            $day    = @(int)$arr_time[2];
            $year   = @(int)$arr_time[0];
            $h      = 0;
            $m      = 0;
            $s      = 0;
            $data['begin_time'] = mktime($h, $m, $s, $month, $day, $year);
        } else {
            $data['begin_time'] = $timestamp;
        }
    }
    // 处理结束时间
    if($data['anytime'] == 1) {
        // 长期有效 
        $data['end_time'] = $G_big_integer;
    } else {
        $arr_time   = explode('-', $data['end_date']);
        if(sizeof($arr_time) == 3) {
            $month  = @(int)$arr_time[1];
            $day    = @(int)$arr_time[2];
            $year   = @(int)$arr_time[0];
            $h      = 23;
            $m      = 59;
            $s      = 59;
            $data['end_time'] = mktime($h, $m, $s, $month, $day, $year);
            if($data['begin_time'] > $data['end_time']) {
                // 如果开始时间大于结束时间，则报错
                $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        '“结束时间”不能小于“开始时间”！', 
                        __FILE__,
                        __LINE__);
            }
        } else {
            $data['end_time']   = $G_big_integer;
            $data['anytime']    = 1;
        }
    }




    $rule   = array(
        'job_name'  => 'i_string|len:1:100', 
        'treatment' => 'i_empty|len:0:100', 
        'tel'       => 'i_empty|len:0:100',
        'link_man'  => 'i_empty|len:0:100',
        'city'      => 'i_empty|len:0:255',
        );
    $def_err    = array(
        'job_name'  => '“职位名称”必须填写，并且不能超过100个字符！', 
        'treatment' => '“工作待遇”不能超过100个字符！', 
        'tel'       => '“联系电话”不能超过100个字符！',
        'link_man'  => '“联系人”不能超过100个字符！',
        'city'      => '“工作地点”不能超过255个字符！',
        );
    $result = data_validator($data, $rule, $def_err);
    if($result !== true) {
        foreach($result as $v) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        $v, 
                        __FILE__,
                        __LINE__);
        }
    }

    if($GLOBALS['ERR']->Count > 0)
        return false;
    else {
        /**
         * 接受数据前的处理工作，例如上传等
         *
         */
        return $data;
    }
}

function add() {
    global $_TABLES, $timestamp;

    $tbl_job= $_TABLES['hr_job'];
    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();

        // 查看系统中是否有同名职位
        $sql = "SELECT COUNT(*) AS `rs_count` FROM $tbl_job 
                        WHERE job_name = '{$result['job_name']}'";
        $rs = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        "当前系统中已经存在职位名为<FONT COLOR=\"red\">{$job_name}</FONT>的职位，无法添加该职位！", 
                        __FILE__,
                        __LINE__);
            return false;
        }
        $result['create_time'] = $timestamp;
        unset($result['id']);
        if($conn->AutoExecute($tbl_job,$result,'INSERT',false,true,true)) {
            return $conn->Insert_ID();
        }
    }
    return false;
}

function modify() {
    global $_TABLES, $timestamp;

    $tbl_job= $_TABLES['hr_job'];
    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();


        // 查看系统中是否有同名职位
        $sql = "SELECT COUNT(*) AS rs_count FROM $tbl_job 
                        WHERE job_name = '{$result['job_name']}'
                          AND id <> '{$result['id']}' ";
        $rs = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        "当前系统中已经存在职位名为<FONT COLOR=\"red\">{$job_name}</FONT>的职位，无法当前修改操作！", 
                        __FILE__,
                        __LINE__);
            return false;
        }
        if($conn->AutoExecute($tbl_job,$result,'UPDATE','id=' . $result['id'] ,true,true)) {
            return $result['id'];
        }
    }
    return false;
}

function del() {
	global $_TABLES;
    $tbl_job = $_TABLES['hr_job'];

    $deltype    = trim(@$_POST['deltype']);
    if($deltype == 'all') {
        // 删除POST传递过来的 item_id 
        // 批量删除
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的删除 单条删除
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();
        $sql    = "DELETE FROM $tbl_job 
                    WHERE id IN ({$str_in}) ";
					
		//echo $sql;
		//exit;			
					
        if($conn->Execute($sql)) {
            js_success('删除指定职位成功！');
        }
    }
}

function appdel() {
	global $_TABLES;
    $tbl_job = $_TABLES['hr_app'];

    $deltype    = trim(@$_POST['deltype']);
    if($deltype == 'all') {
        // 删除POST传递过来的 item_id 
        // 批量删除
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的删除 单条删除
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();
        $sql    = "DELETE FROM $tbl_job 
                    WHERE id IN ({$str_in}) ";
        if($conn->Execute($sql)) {
            //js_success('记录已删除！');
			echo "<script>location='admin.php?mod=hr&action=li&cata=7';</script>";
        }
    }
}


function app($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var;
    
    $view_var   = array();
    $tbl_job    = $_TABLES['hr_job'];
    $tbl_app    = $_TABLES['hr_app'];
    $conn   = & get_db_conn();

    $where      = 'WHERE 1 ';
    // 关键字
    $key    = @trim($_GET['key']);
    if($key != '') {
        $where .= "AND (`job_name` LIKE '%$key%' OR `realname` LIKE '%$key%') ";
    }

    $job_id  = @(int)$_GET['job_id'];
    if($job_id   > 0) {
        $where .= "AND `jobid` = '{$job_id}' ";
    }
    
    // 分页处理开始
    $csql   = "SELECT COUNT(*) AS rs_count FROM $tbl_app $where";
    $rs     = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕
    

    

    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    $sql    = "SELECT * FROM $tbl_app
            $where 
            ORDER BY  $str_order_by id DESC 
            ";
    $rs = $conn->PageExecute($sql,$page_size,$page);
    //echo $sql;
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束


    return $view_var;
}


function appread($id) {
    global $_TABLES, $timestamp;

    $view_var   = array();
    $tbl_app    = $_TABLES['hr_app'];
    if($id > 0) {
        $conn   = & get_db_conn();
        $sql    = "SELECT * FROM $tbl_app WHERE id = $id";
        $view_var   = $conn->GetRow($sql);
    } else {
        $view_var   = array();
    }
    return $view_var;
}
?>